/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2010 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2Image9Tile.h 28 2012-07-28 17:03:06Z trzntaat $
 * $Revision: 28 $
 */

#ifndef T2IMAGE9TILE_H_INCLUDED
#define T2IMAGE9TILE_H_INCLUDED


namespace turska
{

    class Image9Tile
    {
    public:

        enum TILE_ENABLED_ENUM
        {
            TE_TOP_LEFT     = (1 << 0), 
            TE_TOP          = (1 << 1),
            TE_TOP_RIGHT    = (1 << 2),
            TE_LEFT         = (1 << 3),
            TE_CENTER       = (1 << 4),
            TE_RIGHT        = (1 << 5),
            TE_BOTTOM_LEFT  = (1 << 6),
            TE_BOTTOM       = (1 << 7),
            TE_BOTTOM_RIGHT = (1 << 8),
            TE_ALL          = 0x1ff
        };

        Image9Tile(Image *image, I32 leftWidth, I32 centerWidth, I32 rightWidth,
                   I32 topHeight, I32 centerHeight, I32 bottomHeight,
                   bool borderOutside = true, IMAGE_DRAW_MODE drawMode = IDM_NORMAL,
                   UI32 flags = IF_COLOR | IF_ALPHA | IF_ALIGN_TOP_LEFT,
                   UI32 tileEnabledMask = TE_ALL) :
            mImage(image), mLeftWidth(leftWidth), mCenterWidth(centerWidth), mRightWidth(rightWidth),
            mTopHeight(topHeight), mCenterHeight(centerHeight), mBottomHeight(bottomHeight),
            mBorderOutside(borderOutside), mDrawMode(drawMode), mFlags(flags),
            mTileEnabledMask(tileEnabledMask)
        {
            mTargetWidth = (TREAL)getMinimumWidth();
            mTargetHeight = (TREAL)getMinimumHeight();
            mFlags = IF_COLOR | IF_ALPHA | IF_ALIGN_TOP_LEFT;
        }

        inline void setFlags(UI32 flags) { mFlags = flags; }
        inline UI32 getFlags() const { return mFlags; }
        inline void setFlag(UI32 flag) { mFlags |= flag; }
        inline void clearFlag(UI32 flag) { mFlags &= ~flag; }
        inline void setFlagState(UI32 flag, bool enabled)
            { enabled ? setFlag(flag) : clearFlag(flag); }
        inline bool isFlag(UI32 flag) const { return (mFlags & flag) == flag; }

        //! Sets the enabled tiles.
        void setTileEnabledMask(UI32 tileEnabledMask) { mTileEnabledMask = tileEnabledMask; }
        UI32 getTileEnabledMask() const { return mTileEnabledMask; }

        //! Sets drawing mode. \sa IMAGE_DRAW_MODE
        inline void setDrawMode(IMAGE_DRAW_MODE drawMode) { mDrawMode = drawMode; }
        //! Returns current drawing mode. \sa IMAGE_DRAW_MODE
        inline IMAGE_DRAW_MODE getDrawMode() { return mDrawMode; }

        //! Sets the color and alpha to use when IF_COLOR or IF_ALPHA flags are enabled.
        void setColor(const Color &color) { mColor = color; }

        //! Sets target size.
        void setTargetSize(TREAL width, TREAL height) { mTargetWidth = width; mTargetHeight = height; }

        bool isBorderOutside() const { return mBorderOutside; }
        void setBorderOutside(bool borderOutside) { mBorderOutside = borderOutside; }

        //! Returns recommended minimum draw width.
        I32 getMinimumWidth() const { return mLeftWidth + mCenterWidth + mRightWidth; }
        //! Returns recommended minimum draw height.
        I32 getMinimumHeight() const { return mTopHeight + mCenterHeight + mBottomHeight; }

        I32 getLeftWidth() const { return mLeftWidth; }
        I32 getCenterWidth() const { return mCenterWidth; }
        I32 getRightWidth() const { return mRightWidth; }
        I32 getTopHeight() const { return mTopHeight; }
        I32 getCenterHeight() const { return mCenterHeight; }
        I32 getBottomHeight() const { return mBottomHeight; }

        //! Draws image using current flags and target size, with reference point (x,y).
        void draw(TREAL x, TREAL y);

        //! Draws image using given drawing mode, flags, reference point, size and color.
        void draw(IMAGE_DRAW_MODE drawMode, UI32 flags,
                  TREAL x, TREAL y, TREAL width, TREAL height, const Color &color);

    protected:

        Image *mImage;
        I32 mLeftWidth, mCenterWidth, mRightWidth;
        I32 mTopHeight, mCenterHeight, mBottomHeight;
        bool mBorderOutside;   // true if borders are drawn outside target size, false if everything is drawn inside
        IMAGE_DRAW_MODE mDrawMode;
        UI32 mFlags;
        TREAL mTargetWidth, mTargetHeight;
        Color mColor;
        UI32 mTileEnabledMask;

        void drawTopLeftOutside(IMAGE_DRAW_MODE drawMode, UI32 flags,
                                TREAL x, TREAL y, TREAL width, TREAL height, const Color &color);
        void drawTooSmallInside(IMAGE_DRAW_MODE drawMode, UI32 flags,
                                TREAL x, TREAL y, TREAL width, TREAL height, const Color &color);

    }; // class Image9Tile

} // namespace turska


#endif // !T2IMAGE9TILE_H_INCLUDED
